//
//  ZJRegisterSecondViewController.m
//  ZiJinLian
//
//  Created by lzy on 2017/3/10.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ZJRegisterSecondViewController.h"
#import "ZJLoginViewController.h"
#import "AgreementView.h"
@interface ZJRegisterSecondViewController ()<UITextFieldDelegate,agreementDelegate>

@end

@implementation ZJRegisterSecondViewController

-(void)createView{
    [self initTitle:MyString(@"注册")];
    _pswView=[[ZJSetPasswordView alloc]initWithFrame:self.view.frame];
    AgreementView *agree=[[AgreementView alloc]initWithFrame:CGRectMake(0, MainH-64-80, MainW, 20)];
    agree.delegate=self;
    [self.view addSubview:self.pswView];
    [self.view addSubview:agree];
  
}
-(void)AgreementDelegateClick{
    NSLog(@"他同意啦");
}
-(void)doOpeartion{
    self.pswView.firstTF.delegate=self;
    self.pswView.secondTF.delegate=self;
    [self.pswView.sureBtn addTarget:self action:@selector(sureClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.pswView.firstTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self doKeyBoardDown];
}


#pragma -----------输入框代理方法--------
//输入框代理方法
- (void) textFieldDidChange:(id) sender {
    UITextField *_field = (UITextField *)sender;
    NSString *str=_field.text;
    if (str) {
        [self.pswView.sureBtn setBackgroundColor:button_Red_Color];
        [self.pswView.sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
}



-(BOOL) textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    return YES;
}

//确定按钮点击事件
-(void)sureClick:(UIButton *)btn
{
    
     [self.view endEditing:YES];
    //判断 输入框 是否一致。 是否 为空
    //或   ||
    if (![regular checkRegular8:self.pswView.firstTF.text]||![regular checkRegular8:self.pswView.firstTF.text]) {
        [self showMessage:MyString(@"请输入6-20位字母或数字密码")];
        return;
    }
 
    if (![self.pswView.firstTF.text isEqualToString:self.pswView.secondTF.text]) {
         [self showMessage:MyString(@"两次输入密码不一致")];
        return;
    }
 
        NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
        [parametersDic setObject:self.curphoneStr forKey:@"phone"];
        [parametersDic setObject:self.pswView.secondTF.text forKey:@"password"];
        [parametersDic setObject:self.smscodeStr forKey:@"smsCode"];
      [self doHttpRequestWithApi:API_REGIST method:POST dic:parametersDic successBlock:^(NSDictionary *dic) {
          
          [self login];
          
         
      }];
}
-(void)login{
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:self.curphoneStr forKey:@"phone"];
    [parametersDic setObject:self.pswView.secondTF.text forKey:@"password"];
    [self doHttpRequestWithApi:API_LOGIN method:POST dic:parametersDic successBlock:^(NSDictionary *dic) {
        [ZJUserDefaultsTools saveUserInfoLocal:dic];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];

}




@end
